package yndkch.com;

import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.CoordinateXYZM;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * @author QianTanghui
 * @date 2021/1/22 11:03
 */
public class ExportShapefile {

    public static SimpleFeatureType createFeatureType() {


        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();

        builder.setName("buildiong");

        builder.setCRS(DefaultGeographicCRS.WGS84);



        // add attributes in order

        builder.add("the_geom", Point.class);

        builder.length(9).add("ID", Integer.class);

        builder.length(150).add("NAME", String.class);

        builder.length(10).userData("decimalCount",2).add("X", Double.class);

        builder.length(10).userData("decimalCount",3).add("Y", Double.class);

        BigInteger bigInteger = new BigInteger("1234567890123456789");
        BigDecimal bigDecimal = new BigDecimal(bigInteger, 2);

        final SimpleFeatureType BUILDING = builder.buildFeatureType();

        return BUILDING;

    }

    /**
     * 创建Shp文件
     *
     * @param file
     * @param type
     * @throws MalformedURLException
     * @throws IOException
     */
    public static ShapefileDataStore createShapeFile(File file, SimpleFeatureType type) throws MalformedURLException, IOException {
        //输出文件
        Map<String, Serializable> params = new HashMap<>();
        params.put("url", file.toURI().toURL());
        params.put("create spatial index", Boolean.TRUE);
        //创建文件
        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        ShapefileDataStore targetDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
        targetDataStore.setCharset(Charset.forName("GBK"));
        targetDataStore.createSchema(type);
        return targetDataStore;
    }

    private static void writeFeatures(ShapefileDataStore s, SimpleFeatureCollection fc) throws Exception {
        s.createSchema(fc.features().next().getFeatureType());
        try (FeatureWriter<SimpleFeatureType, SimpleFeature> fw =
                     s.getFeatureWriter(s.getTypeNames()[0], Transaction.AUTO_COMMIT);
             SimpleFeatureIterator it = fc.features()) {
            while (it.hasNext()) {
                SimpleFeature feature = it.next();
                SimpleFeature newFeature = fw.next();
                newFeature.setAttributes(feature.getAttributes());

                fw.write();
            }
        }
    }

    public static void testMultiPointZMSupport(){
        // create feature type
        try {
            SimpleFeatureType type =DataUtilities.createType(
                    "junk", "a:MultiPoint,b:java.math.BigDecimal,c:java.math.BigInteger");
            DefaultFeatureCollection features = new DefaultFeatureCollection();

            BigInteger bigInteger = new BigInteger("1234567890123456789");
            BigDecimal bigDecimal = new BigDecimal(bigInteger, 2);

            SimpleFeatureBuilder build = new SimpleFeatureBuilder(type);
            GeometryFactory gf = new GeometryFactory();
            build.add(
                    gf.createMultiPoint(
                            new Point[] {
                                    gf.createPoint(new CoordinateXYZM(1, -1, 1, 2)),
                                    gf.createPoint(new CoordinateXYZM(1, 3, 1, 4)),
                                    gf.createPoint(new CoordinateXYZM(3, 4, 1, 2))
                            }));
            build.add(bigDecimal);
            build.add(bigInteger);

            SimpleFeature feature = build.buildFeature(null);
            features.add(feature);

            // store features
            String file="E:\\变更调查项目\\测试\\new\\test.shp";
            File tmpFile=new File(file);
            tmpFile.createNewFile();
            ShapefileDataStore s = new ShapefileDataStore(tmpFile.toURI().toURL());
            writeFeatures(s, features);
        }catch (Exception e){

        }

    }

    public static void main(String[] args) {
        String file="E:\\变更调查项目\\测试\\new\\test.shp";
        File shpFile=new File(file);

        try {
            SimpleFeatureType simpleFeatureType= createFeatureType();
            ShapefileDataStore store=createShapeFile(shpFile,simpleFeatureType);
            //store.forceSchemaCRS();
            //testMultiPointZMSupport();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
